<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 10/30/12
 * Time: 2:11 PM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Products_DesignerErrorMessages extends Page{
	/**
	 * @var Db_Products_DesignerErrorMessage
	 */
	protected $tableObject;
	function __construct() {
		$this->idTableObject = (get('id', 'int') ? get('id', 'int') : get('childId', 'int'));
		$this->classTableObject = 'Db_Products_DesignerErrorMessage';
		$this->actionsTableObject = array(
			'defaultAction',
			'addEditAction',
			'saveAction'
		);
		parent::__construct();
	}

	public function defaultAction() {
		if(!empty($this->tableObject->objectArray->id)) {
			$this->addBreadcrumb(t($this->tableObject->objectArray->dem_module, $this->tableObject->objectArray->dem_title), '?id='. $this->tableObject->objectArray->id, true);
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit&parentId=' . $this->tableObject->objectArray->id);
		} else {
			self::$DEBUG->addPrivateVar($this->tableObject->objectArray);
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');
		}

		$designerErrorMessages = Db_Products_DesignerErrorMessage::getAllDesignerErrorMessages($this->idTableObject);

		self::$TPL->assign('designerErrorMessages', $designerErrorMessages);
		self::$TPL->setBodyTemplate('list_designer_error_messages');
		return array(
			'type' => 'display'
		);
	}

	public function addEditAction() {
		perm('DESIGNER_ERROR_MESSAGES_EDIT', self::$PAGE['p_url']);
		if(!empty($this->tableObject->objectArray->id) && !get('childId', 'int')) {
			self::addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' "' . t($this->tableObject->objectArray->dem_module, $this->tableObject->objectArray->dem_title) . '"', '?action=addEdit&id=' . $this->tableObject->objectArray->id, true);
			self::$TPL->assign('designerErrorMessages', $this->tableObject->objectArray);
		} else {
			if(get('parentId', 'int')) {
				$designerErrorMessage = new Db_Products_DesignerErrorMessage(get('parentId', 'id'));
				if(!get('childId', 'int')) {
					$this->addBreadcrumb(t($designerErrorMessage->objectArray->dem_module, $designerErrorMessage->objectArray->dem_title), '?id='. $designerErrorMessage->objectArray->id);
					self::addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit&parentId=' . $designerErrorMessage->objectArray->id , true);
				} else {
					$this->addBreadcrumb(t($designerErrorMessage->objectArray->dem_module, $designerErrorMessage->objectArray->dem_title), '?id='. $designerErrorMessage->objectArray->id);
					self::addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' "' . t($this->tableObject->objectArray->dem_module, $this->tableObject->objectArray->dem_title) . '"', '?action=addEdit&parentId=' . $designerErrorMessage->objectArray->id . '&childId=' . $this->tableObject->objectArray->id, true);
					self::$TPL->assign('designerErrorMessages', $this->tableObject->objectArray);
				}

				$parentDesignerErrorMessages = Db_Products_DesignerErrorMessage::getAllDesignerErrorMessages();
				self::$TPL->assign('parents', $parentDesignerErrorMessages);
			} else {
				self::addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true);
			}
		}

		self::$TPL->assign('designerErrorMessages', $this->tableObject->objectArray);

		self::$TPL->setBodyTemplate('add_edit_designer_error_messages');
		return array(
			'type' => 'display'
		);
	}

	public function saveAction() {
		perm('DESIGNER_ERROR_MESSAGES_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');
		$this->tableObject->dem_title = post('dem_title', 'str');
		$this->tableObject->dem_content = post('dem_content', 'str');
		$this->tableObject->dem_parent_id = (post('dem_parent_id', 'int') != 0 ? post('dem_parent_id', 'int') : 0);

		if(!empty($this->tableObject->dem_title)) {
			if(!$this->tableObject->save()) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}
		}
		if($this->tableObject->dem_parent_id != 0) {
			return array(
				'type' => 'redirect',
				'content' => '?id=' . $this->tableObject->dem_parent_id
			);
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

	public function deleteAction() {
		perm('DESIGNER_ERROR_MESSAGES_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');
		if(!Db_Products_DesignerErrorMessage::abstractDelete($this->idTableObject)) {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

}
